package com.corpgovernment.organization.mq;

import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.common.mq.producer.CommonMQProducer;
import com.corpgovernment.organization.Bo.EmployeeOpenCardMQBo;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EmployeeOpenCardMQProducer {

    @Autowired
    private CommonMQProducer commonMQProducer;

    public boolean sendEmployeeOpenCard(EmployeeOpenCardMQBo employeeOpenCardMQBo) {
        try {
            Message message = new Message();
            message.setTopic(MessageBizTypeEnum.EMPLOYEE_OPEN_CARD.getTopic());
            message.setBody(JsonUtils.toJsonString(employeeOpenCardMQBo).getBytes());
            message.setTags(MessageBizTypeEnum.EMPLOYEE_OPEN_CARD.getTags());
            SendResult result = commonMQProducer.send(message);
            if (result != null && result.getSendStatus() == SendStatus.SEND_OK) {
                log.info("员工开卡,emailInfo:{}", employeeOpenCardMQBo);
                return true;
            }
        } catch (Exception e) {
            log.error("员工开卡推送失败:{}", e.getMessage(), e);
        }
        return false;
    }
}
